package com.lanou3g.zjl.chating.home.friends;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupInfo;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/24.
 */
public class AddPublicGroupsActivity extends BaseActivity {
    @Bind(R.id.publicGroupName)
    TextView publicGroupName;//群名称
    @Bind(R.id.publicGroupOwner)
    TextView publicGroupOwner;//群主
    @Bind(R.id.publicGroupIntr)
    TextView publicGroupIntr;//简介
    @Bind(R.id.jionInPublicGruop)
    TextView jionInPublicGruop;//加入按钮

    private EMGroup group;
    private EMGroupInfo groupInfo;

    @Override
    protected int getLayout() {

        return R.layout.addpublicgroup_details;
    }

    @OnClick(R.id.finishAddGroup)
    void finishAddGroup() {
        finish();
    }

    @Override
    protected void initData() {
        groupInfo = (EMGroupInfo) getIntent().getSerializableExtra("groupInfo");
        new Thread(new Runnable() {

            public void run() {
                //从服务器获取详情
                try {
                    group = EMGroupManager.getInstance().getGroupFromServer(groupInfo.getGroupId());

                    runOnUiThread(new Runnable() {
                        public void run() {
                            publicGroupName.setText(groupInfo.getGroupName());
                            //Log.i("lanou", group.getGroupName() + "<><><><>><>" + group.getGroupId());
                            publicGroupOwner.setText(group.getOwner().toString());
                            publicGroupIntr.setText(group.getDescription().toString());
                            //判断该群中是否包含自己,如果包含则不允许加入
                            if (group.getMembers().contains(EMChatManager.getInstance().getCurrentUser())) {
                                jionInPublicGruop.setVisibility(View.GONE);
                            }
                        }
                    });
                } catch (final EaseMobException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(AddPublicGroupsActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        }).start();


    }

    //点击加入群按钮加入群
    @OnClick(R.id.jionInPublicGruop)
    void join() {

        new Thread(new Runnable() {
            public void run() {
                try {
                    //如果是membersOnly的群，需要申请加入，不能直接join
                    if (group.isMembersOnly()) {
                        EMGroupManager.getInstance().applyJoinToGroup(group.getGroupId(), "我是你大哥,你敢不同意???");
                    } else {
                        EMGroupManager.getInstance().joinGroup(group.getGroupId());
                    }
                    runOnUiThread(new Runnable() {
                        public void run() {
                            if (group.isMembersOnly())
                                Toast.makeText(AddPublicGroupsActivity.this, "消息发送成功,请等待验证", Toast.LENGTH_SHORT).show();
                            else
                                Toast.makeText(AddPublicGroupsActivity.this, "加入成功", Toast.LENGTH_SHORT).show();
                            jionInPublicGruop.setEnabled(false);
                        }
                    });
                } catch (final EaseMobException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(AddPublicGroupsActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }
}
